package com.dong.common.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.h2.jdbcx.JdbcDataSource;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * @description:
 * @author: 陈瑞东
 * @create: 2021-08-24 11:22
 */

@Configuration
public class MyBatisPlusConfig{

    /**
     * 配置数据源
     */
    @Bean
    @Lazy(value = false)
    public DataSource dataSource() {
        JdbcDataSource jdbcDataSource = new JdbcDataSource();
//        String dirPath = MyBatisPlusConfig.class.getClassLoader().getResource(".").getPath();
//        String url = "jdbc:h2:" + dirPath + "/db/foodmenu";
        // 测试环境
        String url = "jdbc:h2:./db/foodmenu";
        jdbcDataSource.setURL(url);
        jdbcDataSource.setUser("sa");
        jdbcDataSource.setPassword("");
        return jdbcDataSource;
    }

    /**
     * 乐观锁配置
     * 在实体类字段上加 @version 注解
     *
     * 分页插件配置
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
        return interceptor;
    }


    /**
     * mybatis-plus 配置
     * @return
     */
    @Bean
    public GlobalConfig globalConfig(MetaObjectHandler metaObjectHandler) {
        GlobalConfig globalConfig = new GlobalConfig();
        // 配置逻辑删除
        GlobalConfig.DbConfig dbConfig = new GlobalConfig.DbConfig();
        dbConfig.setLogicDeleteField("is_delete");
        dbConfig.setLogicDeleteValue("1");
        dbConfig.setLogicNotDeleteValue("0");
        globalConfig.setDbConfig(dbConfig);

        // 配置自动填充
        globalConfig.setMetaObjectHandler(metaObjectHandler);

        return globalConfig;
    }

    /**
     * mybatis 配置
     * @return
     */
    @Bean
    public MybatisConfiguration mybatisConfiguration() {
        MybatisConfiguration configuration = new MybatisConfiguration();
        // 开启下划线转驼峰
        configuration.setMapUnderscoreToCamelCase(true);
        // 开启日志
        configuration.setLogImpl(StdOutImpl.class);
        return configuration;
    }

    /**
     * 配置自动填充
     * @return
     */
    @Bean
    public MetaObjectHandler metaObjectHandler() {
        return new MyMetaObjectHandler();
    }
}
